package com.situ.mall.interceptor;

import com.situ.mall.util.JwtUtil;
import com.situ.mall.util.ThreadLocalUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.Map;

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("Authorization");
        if (!ObjectUtils.isEmpty(token)) {
            try {
                Map<String, Object> map = JwtUtil.parseToken(token);
                ThreadLocalUtil.set(map);
                return true;
            } catch (Exception e) {
                return true;
            }
        }
        return true;
    }
}
